<template>
  <div class="background">
    <a-card style="margin-bottom: 10px">
      <a-row>
        <a-col :span="24">
          <a-form
            layout="inline"
            :form="form"
            @submit="handleSubmit"
          >
            <a-form-item label="角色名称">
              <a-input
                allow-clear
                placeholder="请输入角色名称"
                v-model="name"
              ></a-input>
            </a-form-item>
            <a-form-item>
              <a-button
                type="primary"
                html-type="submit"
              >搜索</a-button>
              <a-button
                type="primary"
                style="margin-left: 10px"
                @click="add('')"
              >新增</a-button>
            </a-form-item>
          </a-form>
        </a-col>
      </a-row>
    </a-card>
    <a-card>
      <a-table
        bordered
        :loading="loading"
        :pagination="pagination"
        :columns="columns"
        :data-source="dataSource"
        @change="handleTableChange"
        :scroll="{ x: true }"
      >
        <template
          slot="action"
          slot-scope="txt, record"
        >
          <a
            class="mr-10"
            @click="add(record)"
          >
            <a-icon type="edit" />编辑
          </a>
          <a
            class="mr-10"
            @click="deleteRecord(record)"
          >
            <a-icon type="delete" />删除
          </a>
        </template>
      </a-table>
      <create
        v-if="showAdd"
        :account="account"
        @close="close"
      ></create>
    </a-card>
  </div>
</template>

<script>
import create from './create.vue'
import { postAction } from '@/api/manage'
const columns = [
  {
    title: '角色id',
    dataIndex: 'id',
  },
  {
    title: '角色名称',
    dataIndex: 'name',
  },
  {
    title: '操作',
    width: 130,
    scopedSlots: { customRender: 'action' },
  },
]
export default {
  components: {
    create,
  },
  data() {
    return {
      columns,
      form: this.$form.createForm(this),
      showAdd: false,
      patt: 'api/gateway/basemgt-op',
      account: '',
      name: '',
      loading: true,
      dataSource: [],
      pagination: {
        total: 0,
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30', '40'],
        showQuickJumper: true,
        showSizeChanger: true,
        showTotal: (total) => `共 ${total} 条记录`,
        itemRender: (current, type, originalElement) => {
          if (type === 'prev') {
            return <a>上一页</a>
          } else if (type === 'next') {
            return <a>下一页</a>
          }
          return originalElement
        },
      },
    }
  },
  mounted() {
    this.loadData()
  },

  methods: {
    handleTableChange(pagination) {
      this.pagination = pagination
      this.loadData(1)
    },
    close() {
      this.showAdd = false
      this.loadData()
    },
    add(record) {
      this.showAdd = true
      this.account = record
    },
    update(color) {
      this.color = color
      this.showUpdate = true
    },
    deleteRecord(record) {
      this.$confirm({
        content: '确定要删除' + record.name + '的角色吗？',
        onOk: () => {
          const data = {
            roleId: record.id,
          }
          postAction(`${this.patt}/role/delete`, data).then((data) => {
            if (data.code == 200) {
              this.$message.success('删除成功')
              this.loadData()
            } else {
              this.$message.warning(data.msg)
            }
          })
        },
        onCancel() {},
      })
    },
    handleSubmit(e) {
      e.preventDefault()
      this.loadData()
    },
    loadData(key) {
      const params = {}
      if (key) {
        params.current = this.pagination.current
        params.size = this.pagination.pageSize
      } else {
        params.current = 1
      }
      if (this.name) {
        params.name = this.name
      }
      postAction(`${this.patt}/role/list`, params).then((data) => {
        if (data.code == 200) {
          this.dataSource = data.data.records
          this.pagination.current = data.data.current
          this.pagination.total = data.data.total
        } else {
          this.$message.warning(data.msg)
        }
        this.loading = false
      })
    },
  },
}
</script>
<style lang="less" scoped>
.background {
  background: transparent;
}
.ant-modal-body {
  display: flex;
  justify-content: space-between;
}
</style>